/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.escuela.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.escuela.bl.MaestroBlLocal;
import mx.its.dsw2.escuela.modelo.Maestro;

/**
 *
 * @author Liilie
 */
@ManagedBean
@SessionScoped
public class TeacherBean {

    @EJB
    private MaestroBlLocal maestroBl;
    private Maestro teacher;
    private List<Maestro> lista;
    private String accion;

    public TeacherBean() {
        teacher = new Maestro();
    }

    public Maestro getTeacher() {
        return teacher;
    }

    public void setTeacher(Maestro teacher) {
        this.teacher = teacher;
    }

    public List<Maestro> getLista() {
        lista = maestroBl.getAll();
        return lista;
    }

    //Acciones
    public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            maestroBl.register(teacher);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                maestroBl.modify(teacher);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    maestroBl.delete(teacher);
                }
            }
        }
        accion = "";
        return "TeacherList";
    }

    public String procesarLlamada() {
        String page = "";
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            page = "TeacherCreateEdit";
        } else {
            if (accion.equalsIgnoreCase("Eliminar")) {
                page = "TeacherList";
            }
        }
        return page;
    }

//Listeners
    public void prepararNuevo(ActionEvent evt) {
        accion = "Nuevo";
        teacher = new Maestro();
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        teacher = lista.get(index);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        teacher = lista.get(index);
    }
}
